package com.zjhome.cloudnote.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.zjhome.cloudnote.domain.UserInfo;
import com.zjhome.cloudnote.service.UserService;
import static java.util.Collections.emptyList;

/**
 * 用户详情服务
 * 
 * @author 0283000121
 *
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	@Autowired
	private UserService		userService;	

	/**
	 * 通过构造器注入 UserService
	 * 
	 * @param userService
	 */
	public UserDetailsServiceImpl(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// 
		UserInfo userInfo = null;
		try {
			userInfo = userService.selectUserById(username);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (userInfo == null) {
			throw new UsernameNotFoundException(username);
		}
		
		return new User (
				userInfo.getId(),				
				userInfo.getPassword(), 
				emptyList());
	}	
}
